<?php


    function viewParty(){
        //global $database;

        //$data = $database->viewParty();
        //print_r($data);
//die();
        global $user;
        define("REQ_LEVEL",9);
        if($user->level < REQ_LEVEL){
            Page::$content=UN_AUTH;
         return; 
        }
        $url = "./ajax/party.php";
        $baseLinkUrl = "agricare.php";
        $addParamE = "&page=editparty";
        $addParamD = "&page=delparty";
        $idName = "id";

        $str="<link rel='stylesheet' type='text/css' media='screen' href='./css/redmond/jquery-ui-1.7.1.custom.css' />
            <link rel='stylesheet' type='text/css' media='screen' href='./css/ui.jqgrid.css' />
            <script src='./js/jquery.js' type='text/javascript'></script>
            <script src='./js/i18n/grid.locale-en.js' type='text/javascript'></script>
            <script src='./js/jquery.jqGrid.min.js' type='text/javascript'></script>
            
            <script type='text/javascript'>
            jQuery(document).ready(function(){

            jQuery('#list').jqGrid({
                url:'{$url}',
                datatype: 'xml',
                mtype: 'GET',
                colNames:['SN','Name', 'Address','Phone','Headquater','-','-'],
                colModel :[ 
                {name:'id_profile', index:'id_profile', width:25,sortable:false}, 
                {name:'name', index:'name', width:80, sorttype:'text'}, 
                {name:'address', index:'address', width:80, sorttype:'text'}, 
                {name:'phone', index:'phone', width:80, align:'right',sorttype:'float'}, 
                {name:'headquater', index:'headquater', width:80, sorttype:'text'}, 
                {name:'a', index:'a', width:20, align:'center', sortable:false, formatter:'showlink', formatoptions:{baseLinkUrl:'{$baseLinkUrl}', addParam: '{$addParamE}', idName:'{$idName}'}}, 
                {name:'b', index:'b', width:20, align:'center', sortable:false, formatter:'showlink', formatoptions:{baseLinkUrl:'{$baseLinkUrl}', addParam: '{$addParamD}', idName:'{$idName}'}} 
                ],
                pager: '#pager',
                rowNum:8,
                    width:500,
                    height:150,
                    hidegrid:false,
                rowList:[10,20,30],
                sortname: 'name',
                sortorder: '',
                viewrecords: true,
                imgpath: '',
                caption: 'Party List'
            }); 
            }); 
            </script>
            <table id='list'></table> 
            <div id='pager'></div> 
            ";

            //echo $str."dfjsdjfksdfjs";
            //die();
            Page::$content = $str;
    }

    function addParty(){    
        
        global $form,$ctrl,$database,$user;
        $str;
        define("REQ_LEVEL",9);
        if($user->level < REQ_LEVEL) return UN_AUTH;
        if(isset($_POST['add'])){
            //print_r($_REQUEST);
            //die();
            $args = array("name"=>$_POST['name'],"address"=>$_POST['address'],"phone"=>$_POST['phone'],"headquater_id"=>$_POST['headquater_id']);
            $retval = $ctrl->addParty($args);

            if($retval){
                $str.= "<div class='notice'><font size=\"2\" color=\"#00ff00\">{$_POST['username']}  Data Entered Sucessfully !! </font></div>";
            }
            else{
                if($_SESSION['inp_err']){
                    $form->setValueArray($_POST);
                    $str.="<div class='notice'><font size=\"2\" color=\"#ff0000\">Input error - ".$form->num_errors." error(s) found</font></div>";
                    unset($_SESSION['inp_err']);
                }else{
                    $str.= "Data couldnot be Entered in Database !!";
                }
            }
        }  

            $headquaterList = $database->getHeadquaterList();
            $str .="<div id='heading'><h3>Add Party</h3></div>
                <div><form method='POST' action='{$_SERVER['REQUEST_URI']}'>
                <div>Name : <input type='text' name='name' value=\"{$form->value('name')}\" />{$form->error('name')}</div>
                <div>Address : <input type='text' name='address' value=\"{$form->value('address')}\" />{$form->error('address')}</div>
                <div>Phone : <input type='text' name='phone' value=\"{$form->value('phone')}\" />{$form->error('address')}</div>
                <div>Headquater : <select name='headquater_id'> ";
    
                foreach($headquaterList as $key){
                   $str.= "<option value='{$key['id']}'>{$key['name']}</option>";
                }
    
            $str.= "</select></div>
                <div><input type='hidden' name='add'></div>
                <div><input type='submit' name='submit' value='Add'></div>
                </form></div>";    
            return $str;
    
        //return $str;
    }   

    function editParty(){

        global $form,$database,$user,$ctrl;
        //define("REQ_LEVEL",9);
        //if($user->level < REQ_LEVEL) return UN_AUTH;

        $str;

        if(isset($_SESSION['editpartySuccess'])){
            unset($_SESSION['editpartySuccess']);
            $str.= "<div class='notice'>Data Updated Successfully !!</div>";
        }
        
        if(isset($_POST['edit'])){

            $retval = $ctrl->editParty($_POST);

            if($retval){
                $_SESSION['editpartySuccess'] = true;
                header("Location:".$ctrl->referrer);				
            }
            else{
                $_SESSION['value_array'] = $_POST;
                $_SESSION['error_array'] = $form->getErrorArray();
                header("Location:".$ctrl->referrer);				
            }


        } else {

            $data = $database->getPartyById($_GET['id']);   

           //print_r($data);
            //die();
            $form->setValue('name',$data['name']);            
            $form->setValue('address',$data['address']);          
            $form->setValue('phone',$data['phone']);
            $form->setValue('headquater_id',$data['headquater_id']);
            $form->setValue('party_id',$data['party_id']);
            $form->setValue('profile_id',$data['profile_id']);
            $form->setValue('party_headquater_relation_id',$data['']);
            $form->setValue('address_id',$data['address_id']);
            $form->setValue('phone_id',$data['phone_id']);

            $headquaterList = $database->getHeadquaterList();

            $str .="<div><h4>Edit Party</h4></div>
                <div><form method='POST' action='{$_SERVER['REQUEST_URI']}'>
                <div>Name : <input type='text' name='name' value=\"{$form->value('name')}\" />{$form->error('name')}</div>
                <div>Address : <input type='text' name='address' value=\"{$form->value('address')}\" />{$form->error('address')}</div>
                <div>Phone : <input type='text' name='phone' value=\"{$form->value('phone')}\" />{$form->error('address')}</div>
                <div>Headquater : <select name='headquater_id'> ";
                $selected = "";
                foreach($headquaterList as $key){
                    if($key['id'] == $form->value('headquater_id'))
                        $selected = "selected"; 
                    else
                        $selected = "";
                   $str.= "<option value='{$key['id']}' {$selected}>{$key['name']}</option>";
                }
    
            $str.= "</select></div>
                <div><input type='hidden' name='edit'>
                    <input type='hidden' name='party_id' value='{$form->value('party_id')}'>
                    <input type='hidden' name='profile_id' value='{$form->value('profile_id')}'>
                    <input type='hidden' name='party_headquater_relation_id' value='{$form->value('party_headquater_relation_id')}'>
                    <input type='hidden' name='headquater_id' value='{$form->value('headquater_id')}'>
                    <input type='hidden' name='address_id' value='{$form->value('address_id')}'>
                    <input type='hidden' name='phone_id' value='{$form->value('phone_id')}'>
                </div>
                <div><input type='submit' name='submit' value='Edit'></div>
                </form></div>";    
            return $str;
        }
    }    
?>
